package com.boundless.helmet.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.boundless.helmet.app.MyApplication;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class WriteUtil {


    private static final String TAG = "WriteUtil";

    public static boolean saveUserInfo() {

        try {
            // 判断当前的手机是否有sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)) {
                // 已经挂载了sd卡
                return false;
            }

            File sdCardFile = MyApplication.getInstance().getExternalCacheDir();
            File file = new File(sdCardFile, "writeData.txt");

            FileOutputStream fos = new FileOutputStream(file);

            String currentTime = new Date().toLocaleString();
            String data = currentTime;

            fos.write(data.getBytes());

            //Log.e(TAG, "写入数据时间成功" + currentTime);
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean saveTimeInfo() {

        try {
            // 判断当前的手机是否有sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)) {
                Log.d(TAG, "没有挂载sd卡");
                // 已经挂载了sd卡
                return false;
            }

            File sdCardFile = MyApplication.getInstance().getExternalCacheDir();
            Date currentDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            String formattedDate = dateFormat.format(currentDate);

            File file = new File(sdCardFile, formattedDate + ".txt");

            if (!file.exists()) {
                file.createNewFile();
            }

            Log.d(TAG, "路径xxxxx::" + file.getPath());
            String currentTime = new Date().toLocaleString();
            String data = currentTime;
            // 创建文件写入器
            FileWriter writer = new FileWriter(file, true);
            // 将文本追加到文件末尾
            writer.append(data).append("\n");
            // 关闭写入器
            writer.close();
//            FileOutputStream fos = new FileOutputStream(file);
//

//
//            fos.write(data.getBytes());
//
//            //Log.e(TAG, "写入time数据时间成功" + currentTime);
//            fos.flush();
//            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static boolean startUp() {

        try {
            // 判断当前的手机是否有sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)) {
                Log.d(TAG, "没有挂载sd卡");
                // 已经挂载了sd卡
                return false;
            }

            File sdCardFile = MyApplication.getInstance().getExternalCacheDir();
            Date currentDate = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            String formattedDate = dateFormat.format(currentDate);

            File file = new File(sdCardFile, formattedDate + ".txt");

            if (!file.exists()) {
                file.createNewFile();
            }

            Log.d(TAG, "start up 路径xxxxx::" + file.getPath());
            String currentTime = new Date().toLocaleString();
            String data = currentTime;
            // 创建文件写入器
            FileWriter writer = new FileWriter(file, true);
            // 将文本追加到文件末尾
            writer.append("start up ===>>>>> " + data).append("\n");
            Log.d(TAG, "start up 写入数据 start up ===>>>>> " + data);
            // 关闭写入器
            writer.close();
//            FileOutputStream fos = new FileOutputStream(file);
//

//            fos.write(data.getBytes());
//
//            //Log.e(TAG, "写入time数据时间成功" + currentTime);
//            fos.flush();
//            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static Map<String, String> getUserInfo(Context context) {

        try {
            // 判断当前的手机是否有sd卡
            String state = Environment.getExternalStorageState();

            if (!Environment.MEDIA_MOUNTED.equals(state)) {
                // 已经挂载了sd卡
                return null;
            }

            File sdCardFile = MyApplication.getInstance().getExternalCacheDir();

            File file = new File(sdCardFile, "data.txt");

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String text = br.readLine();

            br.close();

            if (!TextUtils.isEmpty(text)) {
                Map<String, String> userInfoMap = new HashMap<String, String>();
                String[] split = text.split("##");
                userInfoMap.put("number", split[0]);
                userInfoMap.put("password", split[1]);
                return userInfoMap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
